package com.xk.timerapp.utils;

import static androidx.core.content.ContextCompat.getSystemService;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;

import com.xk.timerapp.App;
import com.xk.timerapp.data.AlarmClock;

import java.util.Calendar;

public class AlarmManagerUtil {
    public static final String ALARM_ACTION = "com.loonggg.alarm.clock";



    public static void cancelAlarm(Context context, AlarmClock clock) {
        Intent intent = new Intent(App.instance, AlarmReceiver.class);
        // 确保intent与设置闹钟时的一致，包括所有extras和action等
        intent.putExtra("should_vibrate", clock.vibrate);
        intent.putExtra("ringtone_url", clock.ringtoneUrl);
        intent.putExtra("alarmInterval", clock.alarmInterval);
        intent.putExtra("playtime", clock.playtime);
        intent.putExtra("alarmTimes", clock.alarmTimes);
        PendingIntent pi = PendingIntent.getBroadcast(context, clock.id, intent, PendingIntent
                .FLAG_IMMUTABLE);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pi);
    }


    public static void setDailyAlarm(AlarmClock clock) {
        int hourOfDay = clock.alarmHour;
        int minuteOfHour = clock.alarmMinute;
        AlarmManager alarmManager = (AlarmManager) App.instance.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(App.instance, AlarmReceiver.class);
        intent.putExtra("should_vibrate", clock.vibrate);
        intent.putExtra("ringtone_url", clock.ringtoneUrl);
        intent.putExtra("alarmInterval", clock.alarmInterval);
        intent.putExtra("playtime", clock.playtime);
        intent.putExtra("alarmTimes", clock.alarmTimes);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(App.instance, clock.id,
                intent, PendingIntent.FLAG_IMMUTABLE);

        // 设置闹钟触发的时间为每天的固定时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calendar.set(Calendar.MINUTE, minuteOfHour);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 如果当前时间已经过了设置的闹钟时间，则设置为明天的这个时间
        if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        // 设置闹钟在每天的固定时间触发
        if (alarmManager != null) {
            if(clock.alarmMode.equals("0")){
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            }else if(clock.alarmMode.equals("2")){
                // 每天重复闹钟
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pendingIntent);
            }else{
                // 工作日重复闹钟，设置周一到周五的闹钟
                setRepeatingAlarmOnWeekdays(alarmManager, pendingIntent, calendar,
                        clock.alarmHour, clock.alarmMinute);
            }
        }
    }
    private static void setRepeatingAlarmOnWeekdays(AlarmManager alarmManager,
                                                    PendingIntent pendingIntent, Calendar calendar,
                                                    int hourOfDay, int minuteOfHour) {
        // 设置周一到周五的重复闹钟
        for (int i = Calendar.MONDAY; i <= Calendar.FRIDAY; i++) {
            Calendar c = (Calendar) calendar.clone();
            c.set(Calendar.DAY_OF_WEEK, i);

            // 如果今天已经是周五，且设置的闹钟时间小于当前时间，则需要将闹钟设置到下周一
            if (Calendar.getInstance().get(Calendar.DAY_OF_WEEK) >= c.get(Calendar.DAY_OF_WEEK) &&
                    Calendar.getInstance().after(c)) {
                c.add(Calendar.WEEK_OF_YEAR, 1);
            }

            alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);

            // 为接下来的几天设置闹钟，确保它们在同一周内
            if (i < Calendar.FRIDAY) {
                c.add(Calendar.DATE, 1);
                setRepeatingAlarmOnWeekdays(alarmManager, pendingIntent, c, hourOfDay, minuteOfHour);
            }
        }
    }
    }